package com.jeesite.modules.digitalize.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeesite.modules.digitalize.dao.DigitalizeTaskAnnexDao;
import com.jeesite.modules.digitalize.entity.DigitalizeTaskAnnex;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 关联关系表(DigitalizeTaskAnnex)表服务实现类
 *
 * @author mxz
 * @since 2023-11-15 16:23:44
 */
@Service("digitalizeTaskAnnexService")
public class DigitalizeTaskAnnexService extends ServiceImpl<DigitalizeTaskAnnexDao, DigitalizeTaskAnnex> {

    private String getFileName(String url) {
        if (url != null && url.contains("/")) {
            return url.substring(url.lastIndexOf("/") + 1);
        } else {
            return url;
        }
    }

    public void deleteAndAdd(Long taskId, Long taskDetailId, List<String> fileUrlList) {
        LambdaUpdateChainWrapper<DigitalizeTaskAnnex> eq = this.lambdaUpdate().eq(DigitalizeTaskAnnex::getTaskId, taskId);
        if (taskDetailId == null) {
            eq.isNull(DigitalizeTaskAnnex::getTaskDetailId).remove();
        } else {
            eq.eq(DigitalizeTaskAnnex::getTaskDetailId, taskDetailId).remove();
        }
        if (CollectionUtil.isNotEmpty(fileUrlList)) {
            List<DigitalizeTaskAnnex> collect = fileUrlList.stream().map(item -> new DigitalizeTaskAnnex().setTaskId(taskId).setTaskDetailId(taskDetailId).setFileUrl(item).setFileName(getFileName(item))).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }
}

